// calendar/remindList/remindList.js
const util = require("../../../../utils/util");
const app = getApp();
const dateType = { "1": "工作日", "2": "自然日" };
let startX;
let startY;
let touchStartIndex;
let openid = "";
const localpath = "../../pages/index/";
let toggleCollection = true;
Page({

  /**
   * 页面的初始数据
   */
  data: {
    remindList: [],
    tel: "", // 电话
    showTel: false,
    scroll: false,
  },
  onLoad(options) {
    openid = options.openid;
    // util.colleation(this, "办事提醒日历");
    toggleCollection = true;
  },
  onShow() {
    util.getUrl(app.globalData.url).then(res => {
      const $getUserRemindList = {
        ac: "reminds",
        openid: openid ? openid : ks.getStorageSync("openid"),
      };
      util.bstrequest('calendar.php', $getUserRemindList, "POST").then(res => {
        let allRemind = [];
        try {
          if (res.data) {
            if (res.data.data.remind) {
              let remindList = JSON.parse(JSON.stringify(res.data.data.remind));
              remindList.forEach(v => {
                v.isTouchMove = false;
                v.type = dateType[v.type];
              });
              allRemind = remindList;
            }
            // 已完成办事
            if (res.data.data.complete) {
              let complete = JSON.parse(JSON.stringify(res.data.data.complete));
              if (complete)
                complete.forEach(v => allRemind.unshift(v));
            }
            this.setData({
              remindList: allRemind,
            });
          }
        } catch (e) {
          this.setData({
            remindList: ""
          });
        }
      });
    });
  },
  selectRemind(e) {
    let id = e.currentTarget.dataset.id;
    let data = this.data.remindList.filter(v => v.id === id)[0];
    if (data.ser_link_method === "2") {
      if (data.ser_url)
        this.setData({
          tel: data.ser_url,
          showTel: true,
        });
    } else
      util.click2NextProgram(data, "todayRemind", localpath);
  },
  makePhoneCall() {
    ks.makePhoneCall({
      phoneNumber: this.data.tel,
      success: (result) => {
        this.setData({
          showTel: false,
        });
      },
    });
  },
  close() {
    this.setData({
      showTel: false,
    });
  },
  touchStart(e) {
    touchStartIndex = e.currentTarget.dataset.index;
    startX = e.changedTouches[0].clientX;
    startY = e.changedTouches[0].clientY;
    this.data.remindList.forEach(val => {
      if (val.isTouchMove)
        val.isTouchMove = false;
    });
    this.setData({
      remindList: this.data.remindList,
    });
  },
  touchMove(e) {
    let idx = e.currentTarget.dataset.index;
    let touchMoveX = e.changedTouches[0].clientX;
    let touchMoveY = e.changedTouches[0].clientY;
    let angle = this.angle({ X: startX, Y: startY }, { X: touchMoveX, Y: touchMoveY });
    this.data.remindList.forEach((val, index) => {
      val.isTouchMove = false;
      // 如果移动角度大于30°返回
      if (Math.abs(angle) > 30) return;
      if (index == idx) {
        // 当前触发时间数据
        if (touchMoveX > startX);
        else val.isTouchMove = true;
      }
    });
    this.setData({
      remindList: this.data.remindList,
    });
  },
  angle: function (start, end) {
    var _X = end.X - start.X,
      _Y = end.Y - start.Y;
    return 360 * Math.atan(_Y / _X) / (2 * Math.PI);
  },
  delFootPath(e) {
    let id = e.currentTarget.dataset.id;
    let index = e.currentTarget.dataset.index;
    var $AppletsDelremindList = {
      id,
      ac: "del",
    };
    ks.showModal({
      title: '提示',
      content: '删除该条记录？',
      success: res => {
        if (res.confirm) {
          util.bstrequest('calendar.php', $AppletsDelremindList, "POST").then(res => {
            this.onShow();
          });
        }
      }
    });
  },
  onPageScroll() {
    if (toggleCollection) {
      this.setData({
        scroll: true,
      });
      toggleCollection = false;
    }

  },
  closeCollection() {
    toggleCollection = false;
  },
  openCollection() {
    toggleCollection = true;
  }
})